﻿#include "textticker.h"
#include <QPainter>
#include <QTimer>


TextTicker::TextTicker(QWidget *parent)
    : QLabel(parent)
{
    setMinimumWidth(200);
    setMinimumHeight(40);

    m_curIndex = 0;//当前角码

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &TextTicker::updateIndex);
    timer->start(500);
}

TextTicker::~TextTicker()
{


}

void TextTicker::paintEvent(QPaintEvent *event)
{
    __super::paintEvent(event);
    QPainter painter(this);
    //painter.drawText(0, 30, m_showText.mid(m_curIndex));
    //painter.drawText(width() - m_charWidth * m_curIndex, 30, m_showText.left(m_curIndex));
    painter.drawText(0, 30, m_showText.right(m_showText.length() - m_curIndex));
}


void TextTicker::updateIndex()
{
    if(text() != "")
    {
        m_showText = text();
        setText("");
    }

    m_charWidth = fontMetrics().horizontalAdvance("a");

    update();

    if(fontMetrics().horizontalAdvance(m_showText) > width())
    {
        m_curIndex++;
        if (m_curIndex == m_showText.length())
            m_curIndex = 0;
    }
}
